package com.kingmed.kmss.bpmn.flow.helper;

import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kingmed.kmss.common.bo.bpmn.ProcessDefinitionBO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class RepositoryHelper {

    @Autowired
    RepositoryService repositoryService;


    public RepositoryHelper(RepositoryService repositoryService) {
        super();
        this.repositoryService = repositoryService;
    }

    public Deployment deployProcessDefinitionByClasspathResource(ProcessDefinitionBO deployBO) {
        Deployment deployment = this.repositoryService.createDeployment().category(deployBO.getCategory())
                .key(deployBO.getProcessKey()).addClasspathResource(deployBO.getResourceName()).deploy();

        log.info(" Deployed Process infomation <DeployId-{}, parentId-{}>, deploymentTime<>", deployment.getId(),
                deployment.getParentDeploymentId(), deployment.getDeploymentTime());

        return deployment;
    }

    public Deployment deployProcessDefinitionByInputStream(ProcessDefinitionBO deployBO) {
        Deployment deployment = this.repositoryService.createDeployment().category(deployBO.getCategory())
                .key(deployBO.getProcessKey()).addInputStream(deployBO.getResourceName(), deployBO.getInputStream()).deploy();

        log.info(" Deployed Process infomation <DeployId-{}, parentId-{}>, deploymentTime<>", deployment.getId(),
                deployment.getParentDeploymentId(), deployment.getDeploymentTime());

        return deployment;
    }

    public Deployment deployProcessDefinitionByZipInputStream(ProcessDefinitionBO deployBO) {
        Deployment deployment = this.repositoryService.createDeployment().category(deployBO.getCategory())
                .addZipInputStream(deployBO.getZipInputStream()).deploy();

        log.info(" Deployed Process infomation <DeployId-{}, parentId-{}>, deploymentTime<>", deployment.getId(),
                deployment.getParentDeploymentId(), deployment.getDeploymentTime());

        return deployment;
    }


    public BpmnModel getBpmnModel(String processDefinitionId){
        return repositoryService.getBpmnModel(processDefinitionId);
    }

    public ProcessDefinition getProcessDefinition(String processInstanceId){
        return repositoryService.getProcessDefinition(processInstanceId);
    }
}
